package com.youmeek.ssm.module.category.service.impl;

import com.youmeek.ssm.module.category.model.TaxCode;
import com.youmeek.ssm.module.common.service.INodeHierarchy;

public class TaxCodeNodeHierachyImpl implements INodeHierarchy<TaxCode> {

    @Override
    public boolean isHierarchy(TaxCode pNode, TaxCode sonNode) {
        String pMergeCoding = pNode.getMergeCoding().replaceAll("(00)+$","");
        String sonMergeCoding = sonNode.getMergeCoding().replaceAll("(00)+$","");
        if (pMergeCoding.length() == sonMergeCoding.length() && pMergeCoding == "0") {
            return true;
        }
        return sonMergeCoding.startsWith(pMergeCoding) && sonMergeCoding.length() - pMergeCoding.length() == 2;
    }

    @Override
    public boolean isHierarchyRecursion(TaxCode pNode, TaxCode childrenNode) {
        String pMergeCoding = pNode.getMergeCoding().replaceAll("(00)+$","");
        String sonMergeCoding = childrenNode.getMergeCoding().replaceAll("(00)+$","");
        return sonMergeCoding.startsWith(pMergeCoding) && sonMergeCoding.length() > pMergeCoding.length();
    }

}
